package service;

import common.Message;
import common.MessageType;
import common.User;
import utils.PrintUtil;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author 余一
 * @date 2021/10/25  23:28
 */
public class UserClientService {

    //因为我们可能在其他地方使用user信息，因此做出成员属性
    private User user = new User();
    private Socket socket;

    /**
     * 根据userId和password到服务器验证该用户是否合法
     * @param userId
     * @param password
     * @return
     */
    public boolean checkUser(String userId,String password){
        boolean b = false;
        user.setUserId(userId);
        user.setPassword(password);

        try {
            //连接端口为9999的服务器，将user发送出去
            socket = new Socket(InetAddress.getLocalHost(),9999);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(user);

            //获取服务端响应的数据(Message)
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) ois.readObject();

            //是否登录成功
            if (message.getMesType() .equals(MessageType.MESSAGE_LOGIN_SUCCESS) ) {
                //创建一个和服务器端保持通信的线程 -》 创建一个线程类ClientConnectServerThread
                ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(userId,socket);
                clientConnectServerThread.start();
                //为了后面客户端的发展，我们将线程放入到集合管理
                ThreadManager.addThread(userId,clientConnectServerThread);
                b = true;
            } else {
                //如果登录失败，我们就不能启动和服务器通信的线程
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

    /**
     * 向服务端请求在线用户数据
     */
    public void onlineUser(){
        Message message = new Message();
        message.setSender(user.getUserId());
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_USER);
        try {
            OutputStream outputStream = ThreadManager.getThread(user.getUserId()).getSocket().getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出程序
     */
    public void logout(){
        try {
            OutputStream outputStream = ThreadManager.getThread(user.getUserId()).getSocket().getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            Message message = new Message();
            message.setSender(user.getUserId());
            message.setMesType(MessageType.MESSAGE_CLIENT_EXIT);
            objectOutputStream.writeObject(message);
            System.out.println("用户:" + user.getUserId() + "退出程序");
            System.exit(0);//结束进程
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
